package com.wenjie.gateway.aspect;


import com.wenjie.common.utils.TokenUtil;
import com.wenjie.gateway.aspect.annotations.NoPower;
import com.wenjie.gateway.aspect.annotations.PowerAnnotation;
import com.wenjie.gateway.enums.ClentType;
import com.wenjie.gateway.exception.AuthorityException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @Author wenjie
 * @Date 2023/6/15
 * @Description: TODO 权限校验
 */
@Component
@org.aspectj.lang.annotation.Aspect
public class Aspect {


    @Pointcut("@within(com.wenjie.gateway.aspect.annotations.PowerAnnotation)")
    private void pointcut() {
    }





    /**
     * @Description: TODO 验证用户是否有权限
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2023/6/16
     */
    @Before(value = "pointcut()")
    public void boBefore(JoinPoint joinPoint) {
        Class<?> targetClass = joinPoint.getTarget().getClass();
        PowerAnnotation powerAnnotation = targetClass.getAnnotation(PowerAnnotation.class);
        NoPower annotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(NoPower.class);
        if (annotation != null) return;
        if (powerAnnotation != null) {
            ClentType content = powerAnnotation.value();
            TokenUtil.TokenInfo adminUserNumByToken = TokenUtil.getTokenInfo();
            if (!content.getCode().equals(adminUserNumByToken.getType()))
                throw new AuthorityException("该用户没有权限访问此接口");
        }
    }


}
